// pages/vip/join.js
Page({
    data: {
      joined: false,        // 是否已免费入会
      showSubscribe: false, // 订阅弹窗显隐
      plan: 'month'         // 默认选月付
    },
  
    /* 免费入会 */
    doJoin() {
      wx.showToast({ title: '入会成功', icon: 'success' });
      this.setData({ joined: true }, () => {
        // 成功后 500ms 弹出订阅
        setTimeout(() => this.setData({ showSubscribe: true }), 500);
      });
    },
  
    /* 选择方案 */
    selectPlan(e) {
      this.setData({ plan: e.currentTarget.dataset.plan });
    },
  
    /* 关闭订阅弹窗 */
    closeSubscribe() {
      this.setData({ showSubscribe: false });
    },
  
    /* 立即订阅 */
    doSubscribe() {
      const fee = this.data.plan === 'month' ? 15 : 120;
      const desc = this.data.plan === 'month' ? '月度订阅' : '年度订阅';
  
      // 调起微信支付（这里用云函数或后端统一下单）
      wx.cloud.callFunction({
        name: 'createOrder',
        data: { fee, desc }
      }).then(res => {
        const { timeStamp, nonceStr, package: _package, signType, paySign } = res.result;
        wx.requestPayment({
          timeStamp,
          nonceStr,
          package: _package,
          signType,
          paySign,
          success: () => {
            wx.showToast({ title: '订阅成功', icon: 'success' });
            this.closeSubscribe();
          },
          fail: () => {
            wx.showToast({ title: '支付取消', icon: 'none' });
          }
        });
      });
    },
  
    stopBubble() {} // 阻止冒泡
  });